/*
 * @file_name: http_session.hpp
 * @date: 2021/12/06
 * @author: oaho
 * Copyright @ hz oaho, All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef HTTP_SESSION_HPP
#define HTTP_SESSION_HPP
#include <Network/Buffer.h>
#include <Network/TcpSession.h>
#include <buffer.hpp>
#include <http/http_request.hpp>
#include <http/string_body.hpp>
namespace http {

class http_session : public toolkit::TcpSession {
public:
  using Buffer = toolkit::Buffer;
  using BufferLikeString = toolkit::BufferLikeString;
  using SockException = toolkit::SockException;
  using Ptr = std::shared_ptr<http_session>;
  using Socket = typename toolkit::Socket;
  using string_request_type = http_request<http::string_body>;
  using request_func =
      std::function<void(std::shared_ptr<string_request_type>)>;

public:
  http_session(const Socket::Ptr &sock);

public:
  virtual void onRecv(const Buffer::Ptr &buf) override;
  virtual void onError(const SockException &err) override;
  virtual void onManager() override {}
  virtual size_t input(const void *, size_t);

protected:
  void send_response(std::shared_ptr<http_response<http::string_body>> resp);

private:
  size_t parse_header_internal(std::shared_ptr<string_request_type> &request,
                               const void *data, size_t len);
  void on_recv_http(std::shared_ptr<http::http_request<http::string_body>>);
  void
  serve_m3u8(const std::map<std::string, std::string> &param,
             std::shared_ptr<http::http_request<http::string_body>> request);
  void
  serve_flv(const std::map<std::string, std::string> &param,
            std::shared_ptr<http::http_request<http::string_body>> request);

private:
  BufferLikeString buffer;
};
}; // namespace http

#endif